<template>
  <div class="singer" ref="singer">
    <list-view @select="selectSinger" :list="singers" ref="list"> </list-view>
    <router-view> </router-view>
  </div>
</template>

<script>
  import {getSingerList} from "api/singer";
  import {ERR_OK} from "api/config";
  import Singer from 'common/js/singer'
  import ListView from 'base/listview/listview'
  // 这是一个语法糖 对mutation做一层封装
  import {mapMutations} from 'vuex'
  import {playlistMixin} from 'common/js/mixin'

  const HOT_NAME='热门歌手'
  const HOT_SINGER_LEN=10
  export default {
    mixins: [playlistMixin],
    data(){
      return {
        singers:[]
      }
    },
    created() {
      this._getSingerList()
      // console.log('-created singer',this.singers.length)
    },
    mounted() {
      // console.log("-mounted singer mounted",this.singers.length)
    },
    methods:{
      handlePlaylist(playlist) {
        const bottom = playlist.length > 0 ? '60px' : ''
        this.$refs.singer.style.bottom = bottom
        // console.log(this.$refs.list)
        this.$refs.list.refresh()
      },
      selectSinger(singer) {
        this.$router.push({
          path: `/singer/${singer.id}`
        })
        // 完成了一个队mutation的提交 修改了一个state
        // console.log('vuex setters',singer.name)

        this.setSinger(singer)
      },
      _getSingerList() {
        getSingerList().then((res) => {
          if (res.code === ERR_OK) {
            this.singers = this._formatSinger(res.data.list)
            // console.log("----singer----data",res.data.list)
          }
        }).catch((err) => {
          console.log('promise error', err)
        })
      },
      _formatSinger(list) {
        // 格式化数据，根据字母进行分类
        let map = {
          hot: {
            title: HOT_NAME,
            items: []
          }
        }

        list.forEach((item, index) => {
          // 前十条作为热门数据
          if (index < HOT_SINGER_LEN) {
            map.hot.items.push(new Singer({
              id: item.Fsinger_id,
              name: item.Fsinger_name,
              mid: item.Fsinger_mid
            }))
          }
          const key = item.Findex
          if (!map[key]) {
            map[key] = {
              title: key,
              items: []
            }
          }
          map[key].items.push(new Singer({
            id: item.Fsinger_id,
            name: item.Fsinger_name,
            mid: item.Fsinger_mid
          }))
        })
        // console.log('12312', map)

        // 数据排序，将其放到一个数组中，方便foreach
        let hot = []
        let rest = []
        let other = []
        for (let key in map) {
          let item = map[key]
          if (item.title.match(/[a-zA-Z]/)) {
            rest.push(item)
          } else if (item.title === HOT_NAME) {
            hot.push(item)
          } else {
            // other.push(item)
          }
        }
        rest.sort((a, b) => {
          return a.title.charCodeAt(0)-b.title.charCodeAt(0)
        })
        return hot.concat(rest,other)
      },
      // 扩展运算符 做映射 代码简化
      ...mapMutations({
        setSinger: 'SET_SINGER'
      })
    },
    components: {
      ListView
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .singer
    position: fixed
    top: 88px
    bottom: 0
    width: 100%
</style>
